package dbinit.csv;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashSet;
import java.util.Set;

import core.address.model.continent.Continent;
import core.address.model.country.Country;
import core.address.model.region.Region;
import core.address.model.state.State;
import core.profile.helper.UserType;

import dbinit.InitAddressType;
import dbinit.InitBasicAddress;
import dbinit.InitBasicProfile;
import dbinit.InitBasicRoleRules;
import dbinit.InitBasicRoles;
import dbinit.InitCity;
import dbinit.InitContinent;
import dbinit.InitCountry ;
import dbinit.InitRegion;
import dbinit.InitState;
import dbinit.csv.csvreader.CsvReader;

public class ImportBasicProfile 
{
	/*
	public static void main(String argv[])
	{
		doImport("profile_data1.csv");
	}
	*/
	/**
	 * @param args
	 */
	public static void doImport(String Filename)
	{
		// TODO Auto-generated method stub
		File filePath=new File("../AdvProp/source/",Filename);
	//	String path="../AdvProp/source/";

		try {
			CsvReader profiledata = new CsvReader(filePath.getAbsolutePath());
			
			profiledata.readHeaders();
			while (profiledata.readRecord())
			{
				String firstname=profiledata.get("FirstName");
				String lastname =profiledata.get("LastName");
				String city=profiledata.get("city");
				String state=profiledata.get("state");
				String country=profiledata.get("country");
				String region=profiledata.get("region");
				String continent=profiledata.get("continent");
				String mobile=profiledata.get("mobile");
				String phone=profiledata.get("phone");
				String userId=profiledata.get("userId");
				String role=profiledata.get("Role");
				String gender=profiledata.get("Gneder");
				String anniversary1=profiledata.get("anniversary");
				String birthdaydate1=profiledata.get("birthdaydate");
				String companyname=profiledata.get("companyname");
				String usertype1=profiledata.get("usertype");
				String nickname=profiledata.get("nickname");
				String title=profiledata.get("title");
				String creationdate1=profiledata.get("creationdate");
				String status1=profiledata.get("status");
				String emailId=profiledata.get("emailId");
				String currentAddress=profiledata.get("currentAddress");
				String secondname=profiledata.get("secondname");
				String fax=profiledata.get("fax");
				String homepage=profiledata.get("homepage");
				String postalcode=profiledata.get("postalcode");
				String phonebusiness=profiledata.get("phonebusiness");
				//String rolerulename=profiledata.get("rolerulename");
				//String roledesc=profiledata.get("roledesc");
				//String premissiontype=profiledata.get("premissiontype");
				//String street=profiledata.get("street");
	         	  Calendar cal = Calendar.getInstance();
				  SimpleDateFormat formatter=new SimpleDateFormat("dd/MM/yy");


				  
				  Date anniversary2 = null; 
				  Date birthdaydate2 = null;
				  Date creationdate2 = null;
				  
				  java.sql.Date anniversary = null;
				  java.sql.Date birthdaydate = null;
				  java.sql.Date creationdate = null;
				  
				  if(anniversary1 != null && anniversary1.trim()!= "" )
				  {
					  anniversary2 = (Date)formatter.parse(anniversary1);
					  anniversary = new java.sql.Date(anniversary2.getTime());
				  }
				  if(birthdaydate1 != null && birthdaydate1.trim()!= "" )
				  {
					  birthdaydate2 = (Date)formatter.parse(birthdaydate1); 
					  birthdaydate = new java.sql.Date(birthdaydate2.getTime());
				  }
				  if(birthdaydate1 != null && birthdaydate1.trim()!= "" )
				  {
					  creationdate2 = (Date)formatter.parse(creationdate1);
					  creationdate = new java.sql.Date(creationdate2.getTime());
				  }
	  
				  int usertype =UserType.getUserTypeId(usertype1); 
				  boolean status = Boolean.parseBoolean(status1); 	  
				  
				  Long addresscurrenttypeId = InitAddressType.doinit("currentAddress");
				  Long addresstpermanentypeId = InitAddressType.doinit("permanentAddress");
				  Long continentId = InitContinent.doinit(continent);
				  Continent conti = InitContinent.getContinentByName(continent);
				  Country coun = InitCountry.getCountryByName(country);
				  Long countryId = null ;
				  if(coun == null)
				  {
					  countryId = InitCountry.doinit(country,conti,"");
					  coun = InitCountry.getCountryByUuid(countryId);
				  }
				  else
				  {
					  countryId = coun.getUuid();
				  }
				  State s = InitState.getStateByName(state);
				  Long stateId = null ;
				  if(s == null)
				  {
					  stateId = InitState.doinit(coun ,state,conti,"");
				  }
				  else
				  {
					  stateId = s.getUuid();
				  }
				 
				  Long rregionId = InitRegion.doinit(region, s, coun, conti);
				  Region r = InitRegion.getRegionByName(region);
				  Long cityId = InitCity.doinit(s, r ,coun , conti ,city);
				  
				  String addressLine1=currentAddress+ " " + city;
				  String addressLine2=postalcode+countryId;
				  //Start of role
				  Long basicrolerulesId = null ;
				  if(role.equals("Property Owner"))
				  {
					   basicrolerulesId = InitBasicRoleRules.doinit("Edit Own Product","He can edit his product","edit_his_product");
				  }
					//Long basicrolerulesId2 = InitBasicRoleRules.doinit("Edit All Product","He can edit All product","product_manager");
					Set <Long> basicRoleRules = new HashSet<Long>();
					basicRoleRules.add(basicrolerulesId);

					/**
					 * class BasicRoles table basicroles
					 * Long doinit(Long organizationId,String roleName)
					 */
					Set <Long> basicRoles = new HashSet<Long>();
					Long basicroleId2 = InitBasicRoles.doinit("Owner",basicRoleRules);
					basicRoles.add(basicroleId2);
				 //end of role
					Long currentAddressId = InitBasicAddress.doinit(addresscurrenttypeId,addressLine1, addressLine2, phone, mobile, emailId, fax, homepage, postalcode, phonebusiness, companyname, firstname, secondname, lastname, title, cityId, stateId, countryId);
				    Long permanentAddressId = InitBasicAddress.doinit(addresstpermanentypeId,addressLine1, addressLine2, phone, mobile, emailId, fax, homepage, postalcode, phonebusiness, companyname, firstname, secondname, lastname, title, cityId, stateId, countryId);
					
					
				    Long basicprofileId = InitBasicProfile.doinit(anniversary, birthdaydate, companyname, firstname, gender, usertype, lastname, nickname, secondname, title, userId, creationdate, status, emailId, currentAddressId, permanentAddressId, basicRoles);
			}
			
			  profiledata.close();
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	

}

